<template>
	<view class="login-container">
		<view class="login-card">
			<view class="title-container">
				<view class="title-line"></view>
				<text class="title-text">注册</text>
			</view>

			<!-- 表单 -->
			<uv-form class="form" :rules="rules" :model="form" ref="formRef" label-position="top">
				<view class="input-container" :class="{'error': studentIdError}">
					<view class="input-icon">
						<text class="iconfont bs-my"></text>
					</view>
					<uv-form-item prop="studentId" class="form-item">
						<uv-input class="login-input" :placeholder="studentIdError ? '请输入学号' : '请输入学号'"
							:placeholder-style="studentIdError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.studentId"
							@blur="validateStudentId"
							confirm-type="next"
							@confirm="focusPassword" />
					</uv-form-item>
				</view>
				<view class="input-container" :class="{'error': passwordError}">
					<view class="input-icon">
						<text class="iconfont bs-password"></text>
					</view>
					<uv-form-item prop="password" class="form-item">
						<uv-input class="login-input" :placeholder="passwordError ? '请输入密码' : '请输入密码'"
							:placeholder-style="passwordError ? 'color: #ff4d4f;' : ''"
							border="none" type="password"
							v-model="form.password"
							ref="passwordInput"
							@blur="validatePassword" />
					</uv-form-item>
				</view>
				<view class="input-container" :class="{'error': passwordRepeatError}">
					<view class="input-icon">
						<text class="iconfont bs-password"></text>
					</view>
					<uv-form-item prop="passwordRepeat" class="form-item">
						<uv-input class="login-input" :placeholder="passwordRepeatError ? '请再次输入密码' : '请再次输入密码'"
							:placeholder-style="passwordRepeatError ? 'color: #ff4d4f;' : ''"
							border="none" type="password"
							v-model="form.passwordRepeat"
							@blur="validatePasswordRepeat" />
					</uv-form-item>
				</view>
				<view class="input-container" :class="{'error': nameError}">
					<view class="input-icon">
						<text class="iconfont bs-name"></text>
					</view>
					<uv-form-item prop="name" class="form-item">
						<uv-input class="login-input" :placeholder="nameError ? '请输入姓名' : '请输入姓名'"
							:placeholder-style="nameError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.name"
							@blur="validateName" />
					</uv-form-item>
				</view>
				<view class="input-container" :class="{'error': gradeError}">
					<view class="input-icon">
						<text class="iconfont bs-garde"></text>
					</view>
					<uv-form-item prop="grade" class="form-item">
						<uv-input @click="openGradePicker" class="login-input" 
							:placeholder="gradeError ? '请选择年级' : '请选择年级'"
							:placeholder-style="gradeError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.grade"
							@blur="validateGrade" />
					</uv-form-item>
				</view>
				<uv-picker ref="gradePicker" :columns="gradeColumns" @confirm="onGradeConfirm" />

				<view class="input-container" :class="{'error': collegeError}">
					<view class="input-icon">
						<text class="iconfont bs-xueyuan"></text>
					</view>
					<uv-form-item prop="college" class="form-item">
						<uv-input @click="openCollegePicker" class="login-input" 
							:placeholder="collegeError ? '请选择学院' : '请选择学院'"
							:placeholder-style="collegeError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.college"
							@blur="validateCollege" />
					</uv-form-item>
				</view>
				<uv-picker ref="collegePicker" :columns="collegeColumns" @confirm="onCollegeConfirm" />

				<view class="input-container" :class="{'error': classNameError}">
					<view class="input-icon">
						<text class="iconfont bs-class"></text>
					</view>
					<uv-form-item prop="className" class="form-item">
						<uv-input class="login-input" :placeholder="classNameError ? '请输入班级' : '请输入班级'"
							:placeholder-style="classNameError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.className"
							@blur="validateClassName" />
					</uv-form-item>
				</view>
				<view class="input-container" :class="{'error': phoneError}">
					<view class="input-icon">
						<text class="iconfont bs-phone"></text>
					</view>
					<uv-form-item prop="phone" class="form-item">
						<uv-input class="login-input" :placeholder="phoneError ? '请输入电话' : '请输入电话'"
							:placeholder-style="phoneError ? 'color: #ff4d4f;' : ''"
							border="none" v-model="form.phone"
							@blur="validatePhone" />
					</uv-form-item>
				</view>
			</uv-form>

			<!-- uv-toast组件 -->
			<uv-toast ref="toast" />

			<!-- 注册按钮 -->
			<view class="btn-container">
				<uv-button type="primary" size="large" text="注册" @click="handleRegister" shape="circle"
					class="login-btn" hover-class="btn-hover"></uv-button>
			</view>

			<view class="switch-mode" @click="goToLogin">
				<text class="left">已有账号？</text>
				<text class="right">点击登录</text>
			</view>
		</view>

		<!-- 背景动画元素 -->
		<view class="circle circle-1"></view>
		<view class="circle circle-2"></view>
		<view class="circle circle-3"></view>
	</view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import db from '@/api/database'

const toast = ref()
const router = useRouter()
const passwordInput = ref(null)

const form = reactive({
	studentId: '',
	password: '',
	passwordRepeat: '',
	name: '',
	grade: '',
	college: '',
	className: '',
	phone: ''
})

const formRef = ref(null)
const gradePicker = ref(null)
const collegePicker = ref(null)
const gradeColumns = ref([['2020', '2021', '2022', '2023', '2024', '2025']])
const collegeColumns = ref([['信息科学与工程学院', '物理学院', '生物科学学院', '化学学院', '地理科学与环境学院']])

// 错误状态
const studentIdError = ref(false)
const passwordError = ref(false)
const passwordRepeatError = ref(false)
const nameError = ref(false)
const gradeError = ref(false)
const collegeError = ref(false)
const classNameError = ref(false)
const phoneError = ref(false)

// 验证函数
const validateStudentId = () => {
	studentIdError.value = !form.studentId
}

const validatePassword = () => {
	passwordError.value = !form.password
}

const validatePasswordRepeat = () => {
	passwordRepeatError.value = !form.passwordRepeat || form.password !== form.passwordRepeat
}

const validateName = () => {
	nameError.value = !form.name
}

const validateGrade = () => {
	gradeError.value = !form.grade
}

const validateCollege = () => {
	collegeError.value = !form.college
}

const validateClassName = () => {
	classNameError.value = !form.className
}

const validatePhone = () => {
	phoneError.value = !form.phone || !/^1[3-9]\d{9}$/.test(form.phone)
}

const focusPassword = () => {
	passwordInput.value?.focus()
}

const rules = {
	studentId: [{ 
		required: true,
		validator: (rule, value, callback) => {
			studentIdError.value = !value
			callback()
		}
	}],
	password: [{ 
		required: true,
		validator: (rule, value, callback) => {
			passwordError.value = !value
			callback()
		}
	}],
	passwordRepeat: [
		{
			required: true,
			validator: (rule, value, callback) => {
				passwordRepeatError.value = !value || value !== form.password
				callback(passwordRepeatError.value ? new Error('两次输入的密码不一致') : undefined)
			}
		}
	],
	name: [{ 
		required: true,
		validator: (rule, value, callback) => {
			nameError.value = !value
			callback()
		}
	}],
	grade: [{ 
		required: true,
		validator: (rule, value, callback) => {
			gradeError.value = !value
			callback()
		}
	}],
	college: [{ 
		required: true,
		validator: (rule, value, callback) => {
			collegeError.value = !value
			callback()
		}
	}],
	className: [{ 
		required: true,
		validator: (rule, value, callback) => {
			classNameError.value = !value
			callback()
		}
	}],
	phone: [
		{
			required: true,
			validator: (rule, value, callback) => {
				phoneError.value = !value || !/^1[3-9]\d{9}$/.test(value)
				callback(phoneError.value ? new Error('电话号码格式不正确') : undefined)
			}
		}
	]
}

function handleRegister() {
	validateStudentId()
	validatePassword()
	validatePasswordRepeat()
	validateName()
	validateGrade()
	validateCollege() 
	validateClassName()
	validatePhone()

	if (!form.studentId || !form.password || !form.passwordRepeat || !form.name || 
		!form.grade || !form.college || !form.className || !form.phone) {
		return
	}

	formRef.value?.validateField(['studentId', 'password', 'passwordRepeat', 'name', 'grade', 'college', 'className', 'phone'], async (valid) => {
		if (valid && valid.length === 0) {
			try {
				await db.collection('grxx').add({
					xh: form.studentId,
					mm: form.password,
					name: form.name,
					nj: form.grade,
					xy: form.college,
					bj: form.className,
					phone: form.phone,
					sfsf: '本科生'
				})
				toast.value?.show({ message: '注册成功', type: 'success' })
				setTimeout(() => {
					router.push('/pages/login/login')
				}, 1000)
			} catch (err) {
				toast.value?.show({ message: '注册失败，请稍后再试', type: 'error' })
			}
		} else {
			console.log('表单验证失败', valid)
		}
	})
}

function goToLogin() {
	router.push('/pages/login/login')
}

function openGradePicker() {
	gradePicker.value?.open()
}

function onGradeConfirm(val) {
	form.grade = val.value[0]
}

function openCollegePicker() {
	collegePicker.value?.open()
}

function onCollegeConfirm(val) {
	if (Array.isArray(val.value) && val.value.length > 0) {
		form.college = val.value[0]
	}
}
</script>

<style lang="scss" scoped>
@import "/static/css/iconfont.css";

/* 关键帧动画定义 */
@keyframes float {
	0% {
		transform: translateY(0px);
	}

	50% {
		transform: translateY(-20px);
	}

	100% {
		transform: translateY(0px);
	}
}

@keyframes pulse {
	0% {
		transform: scale(1);
		opacity: 0.8;
	}

	50% {
		transform: scale(1.05);
		opacity: 0.6;
	}

	100% {
		transform: scale(1);
		opacity: 0.8;
	}
}

@keyframes fadeIn {
	from {
		opacity: 0;
		transform: translateY(20px);
	}

	to {
		opacity: 1;
		transform: translateY(0);
	}
}

.login-container {
	width: 100%;
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
	overflow: hidden;
	position: relative;
}

.login-card {
	width: 92%;
	max-width: 600rpx;
	background-color: rgba(255, 255, 255, 0.9);
	border-radius: 20px;
	padding: 40rpx 30rpx;
	box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
	z-index: 10;
	animation: fadeIn 0.8s ease-out;
	position: relative;
}

.title-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-bottom: 20px;
	position: relative;
}

.title-line {
	width: 60px;
	height: 4px;
	background: linear-gradient(90deg, #22c4ff, #2196f3);
	border-radius: 2px;
	margin-bottom: 10px;
	transition: width 0.3s ease;
}

.title-text {
	font-size: 40rpx;
	font-weight: bold;
	color: #333;
	position: relative;

	&::after {
		content: '';
		position: absolute;
		bottom: -8px;
		left: 0;
		width: 100%;
		height: 3px;
		background: linear-gradient(90deg, #22c4ff, #2196f3);
		transform: scaleX(0);
		transition: transform 0.3s ease;
	}

	&:active::after {
		transform: scaleX(1);
	}
}

.form {
	width: 90%;
	margin: 0 auto;
}

.input-container {
	display: flex;
	align-items: center;
	background-color: #f5f7fa;
	border-radius: 50rpx;
	margin-bottom: 20rpx;
	transition: all 0.3s ease;
	box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
	border: 1px solid rgba(255, 255, 255, 0.5);

	&:active {
		box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
	}

	&:focus-within {
		box-shadow: 0 5px 15px rgba(34, 196, 255, 0.2);
		border: 1px solid rgba(34, 196, 255, 0.5);
	}

	&.error {
		border: 1px solid #ff4d4f;
		box-shadow: 0 2px 10px rgba(255, 77, 79, 0.1);
	}
}

.input-icon {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 60rpx;
	height: 60rpx;
	color: #22c4ff;
	margin-left: 10px;
	font-size: 40rpx;

	text{
		font-size: 36rpx;
	}
}

.form-item {
	flex: 1;
}

.login-input {
	font-size: 30rpx;
}

.btn-container {
	margin: 40rpx auto 20rpx;
	width: 85%;
}

.login-btn {
	background: linear-gradient(45deg, #22c4ff, #2196f3) !important;
	box-shadow: 0 10px 20px rgba(34, 196, 255, 0.3);
	transition: all 0.3s ease !important;
	border: none !important;
	border-radius: 50rpx !important;
	font-size: 32rpx;
	font-weight: bold;
	letter-spacing: 4rpx;
}

.btn-hover {
	transform: translateY(-3px);
	box-shadow: 0 15px 25px rgba(34, 196, 255, 0.4);
	opacity: 0.9;
}

.switch-mode {
	z-index: 1;
	font-size: 28rpx;
	margin-top: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;
	padding: 10rpx 0;

	.left {
		color: #8a9099;
	}

	.right {
		color: #22c4ff;
		margin-left: 10rpx;
		font-weight: bold;
		transition: all 0.3s ease;

		&:active {
			color: #2196f3;
			transform: scale(1.05);
		}
	}

	&:active {
		transform: translateY(-2px);
	}
}

/* 背景动画圆圈 */
.circle {
	position: absolute;
	border-radius: 50%;
	background: linear-gradient(45deg, rgba(34, 196, 255, 0.2), rgba(33, 150, 243, 0.2));
	filter: blur(5px);
}

.circle-1 {
	width: 500rpx;
	height: 500rpx;
	top: -150rpx;
	right: -100rpx;
	animation: pulse 8s infinite ease-in-out;
}

.circle-2 {
	width: 350rpx;
	height: 350rpx;
	bottom: 100rpx;
	left: -100rpx;
	animation: pulse 6s infinite ease-in-out 1s;
}

.circle-3 {
	width: 600rpx;
	height: 600rpx;
	bottom: -300rpx;
	right: 30%;
	animation: pulse 10s infinite ease-in-out 2s;
}
</style>